﻿/* 2013/1/6 */
using System;
using System.IO;

namespace Cosmos.Media.Play.IO
{
    /// <summary>
    /// Represents a reader that loads a play list from a M3U file.
    /// </summary>
    public class M3uReader : IPlayListReader, IDisposable
    {
        private const string _EXTM3U = "#EXTM3U";
        private const string _EXTINF = "#EXTINF:";

        private const string _TimeDetailKey = "Time";

        private FileStream _fileStream;
        private TextReader _textReader;
        private string _fileDirectory;

        /// <summary>
        /// Initializes a new instance of M3uReader class with specified M3U file path.
        /// </summary>
        /// <param name="path">Path of the M3U file.</param>
        public M3uReader(string path)
        {
            _fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
            _textReader = new StreamReader(_fileStream);

            _fileDirectory = Path.GetDirectoryName(path);
        }

        public void Close()
        {
            if (_textReader != null)
            {
                _textReader.Close();
            }

            if (_fileStream != null)
            {
                _fileStream.Close();
            }
        }

        public void Dispose()
        {
            if (_textReader != null)
            {
                _textReader.Dispose();
            }

            if (_fileStream != null)
            {
                _fileStream.Dispose();
            }
        }

        public PlayList Load()
        {
            PlayList playList = new PlayList();
            string textLine = _textReader.ReadLine();
            string[] infoStrings = null;
            string musicPath = null;
            PlayListItem playItem = null;
            while ((textLine = _textReader.ReadLine()) != null)
            {
                if (textLine.StartsWith(_EXTINF))
                {
                    // There is information before file path.
                    infoStrings = textLine.Substring(_EXTINF.Length).Split(new char[] { ',' });
                }
                else
                {
                    continue;
                }

                musicPath = _textReader.ReadLine();
                if (!File.Exists(musicPath))
                {
                    // The path is not an available absolute path.
                    musicPath = Path.Combine(_fileDirectory, musicPath);
                    if (!File.Exists(musicPath))
                    {
                        // The path is not an available relative path.
                        musicPath = null;
                    }
                }

                playItem = new PlayListItem(infoStrings[1], musicPath);                
                //playItem.Details.Add(_TimeDetailKey, infoStrings[0]);
                PlayAttribute timeAttribute = new PlayAttribute(_TimeDetailKey, infoStrings[0]);
                playItem.Attributes.Add(timeAttribute);

                playList.Items.Add(playItem);
            }

            return playList;
        }
    }
}
